package app.r_teenus.backend.dao;


import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import app.r_teenus.backend.model.Contact;
import app.r_teenus.backend.model.Employee;
import app.r_teenus.backend.model.Enterprise;
import app.r_teenus.backend.model.Person;


@Repository("employeeDao")
@Transactional(readOnly = true)
public class EmployeeDaoImpl extends DefaultDaoImpl implements EmployeeDao {

//	private HibernateTemplate hibernateTemplate;
//
//	public void setSessionFactory(SessionFactory sessionFactory) {
//		hibernateTemplate = new HibernateTemplate(sessionFactory);
////		setHibernateTemplate(hibernateTemplate);
//	}

	public Employee findEmployeeById(Integer id) {
		hibernateTemplate.getSessionFactory().getCurrentSession().enableFilter("SUBJECT_TYPE").setParameter("SUBJECT_TYPE", 1);
		hibernateTemplate.getSessionFactory().getCurrentSession().enableFilter("SUBJECTATTRIBUTE_TYPE").setParameter("SUBJECTATTRIBUTE_TYPE", 1);
		hibernateTemplate.getSessionFactory().getCurrentSession().enableFilter("CONTACT_TYPE").setParameter("CONTACT_TYPE", 1);
		Employee employee = hibernateTemplate.get(Employee.class, id);
		SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
		if (employee.getPerson().getBirthDate() != null) {
			employee.getPerson().setBirthDateString(df.format(employee.getPerson().getBirthDate()));
		}
		hibernateTemplate.initialize(employee.getPerson().getAddresses());
		hibernateTemplate.initialize(employee.getPerson().getContacts());
//		employee.getPerson().getSubjectAttributes().size();
//		employee.getPerson().getAddresses().size();
//		employee.getPerson().getContacts().size();
		return employee;
	}

	/**
	 * Saves address to person. //
	 */
	@Transactional(readOnly = false)
	public Employee saveContact(Employee employee, Contact contact) {
		contact.setContactType(findContactTypeById(contact.getContactTypeId()));
		if (contact.getId() != null) {
			for (Contact cont : employee.getPerson().getContacts()) {
				if (cont.getId().intValue() == contact.getId().intValue()) {
					employee.getPerson().getContacts().remove(cont);
					break;
				}
			}
		} else {
			contact.setSubject(employee.getPerson().getId());
			contact.setSubjectType(1);
		}
		employee.getPerson().getContacts().add(contact);

		hibernateTemplate.saveOrUpdate(contact);
		return employee;
	}

	public List<Employee> findEmployees() {
		return hibernateTemplate.loadAll(Employee.class);
	}

	public Employee createNewEmployee() {
		Employee employee = new Employee();
		employee.setPerson(new Person());
		employee.setEnterprise(new Enterprise());
		return employee;
	}

	/**
	 * Deletes contact from person.
	 */
	@Transactional(readOnly = false)
	public Employee deleteContact(Employee employee, Integer contactId) {
		for (Contact cont : employee.getPerson().getContacts()) {
			if (cont.getId().intValue() == contactId.intValue()) {
				hibernateTemplate.delete(cont);
				employee.getPerson().getContacts().remove(cont);
				break;
			}
		}
		return employee;
	}

	public Employee save(Employee employee) {
		// TODO Auto-generated method stub
		return null;
	}

	public void delete(Employee employee) {
		// TODO Auto-generated method stub

	}
}
